<template>
	<div class="flex box-row-column">
		<el-form :inline="true">
			<el-row>
				<el-col :span="12">
					<el-form-item label="阶段状态:" style="width: 80%">
						<div style="color: #666">
							{{
								props.datainfo.progressColor == 'green' || props.datainfo.progressColor == ''
									? '正常'
									: props.datainfo.progressColor == 'orange' || props.datainfo.progressColor == 'red'
										? '滞后'
										: ''
							}}
						</div>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<div class="flex box-row-column">
			<div class="titlezi flex">
				<div class="flex-1">会前附件</div>
			</div>

			<div class="kuaibj mt20">
				<Cfupload
					v-for="value in fjcodelist"
					:key="value"
					:istable="false"
					:labeltitle="value.definitionName"
					:code="value.businessCode"
					:suffix-type="value.suffixType"
					:disabled="geteditbefore"
					:ismust="value.imustUpload"
					:filelist="value.uploadedFiles"
					@success="handleUploadSuccessbefore"
					@remove="handleFileRemovebefore"
				></Cfupload>
				<!-- <div class="titlezi flex flex-align-center">
				<div class="">征求意见函填写</div>
				<div class="color1491FF font14 ml60 cursor" @click="hanvisible = true" v-if="Number(props.requestParams.progressStatus) == 8">编辑</div>
			</div> -->
			</div>
		</div>

		<el-tabs v-model="activeName" class="demo-tabs">
			<el-tab-pane label="主编单位信息" name="first">
				<el-button v-if="geteditbefore" type="primary" small @click="addzhubian">新增</el-button>
				<el-table :data="queryParams.mainUnitList" stripe class="mt20">
					<el-table-column label="序号" align="center" type="index" width="120">
						<template #default="{ row }">
							{{ `第${row.orderName}${'主编单位'}` }}
						</template>
					</el-table-column>
					<el-table-column label="主编单位名称" align="center" prop="editorUnitName"> </el-table-column>
					<el-table-column label="企业社会信用代码" align="center" prop="userCode"></el-table-column>
					<el-table-column label="联系人" align="center" prop="contacts"></el-table-column>
					<el-table-column label="联系电话(手机)" align="center" prop="contactsTel"></el-table-column>
					<el-table-column label="联系人邮箱" align="center" prop="contactsEmail"></el-table-column>
					<el-table-column label="联系人地址" align="center" prop="contactsAddress"></el-table-column>
					<el-table-column label="负责人" align="center" prop="responsiblePerson"></el-table-column>
					<el-table-column label="负责人电话(手机)" align="center" prop="responsiblePersonTel"></el-table-column>
					<el-table-column label="负责人邮箱" align="center" prop="responsiblePersonEmail"></el-table-column>
					<el-table-column v-if="geteditbefore" label="操作" fixed="right" header-align="center" align="center" width="90">
						<template #default="{ row, $index }">
							<!-- <el-icon style="color: #1491ff"><EditPen /></el-icon> -->
							<el-icon v-if="row.isedit" :size="16" color="#1491FF" class="cursor" @click="goedit(row, $index)"> <Edit /></el-icon>
							<el-icon v-if="row.isedit" class="ml20 cursor" :size="16" style="color: #fe6360" @click="removeli_zb($index)"><Delete /></el-icon>
						</template>
					</el-table-column>
				</el-table>
			</el-tab-pane>
			<el-tab-pane label="参编单位信息" name="second">
				<el-button v-if="geteditbefore" type="primary" small @click="addcanbian">新增</el-button>
				<el-table :data="queryParams.partUnitList" stripe class="mt20">
					<el-table-column label="序号" align="center" type="index" width="120">
						<template #default="{ row }">
							{{ `第${row.orderName}${'参编单位'}` }}
						</template>
					</el-table-column>
					<el-table-column label="参编单位名称" align="center" prop="editorUnitName"></el-table-column>
					<el-table-column label="企业社会信用代码" align="center" prop="userCode"></el-table-column>
					<el-table-column label="联系电话(手机)" align="center" prop="contactsTel"></el-table-column>
					<el-table-column label="联系人邮箱" align="center" prop="contactsEmail"></el-table-column>
					<el-table-column label="联系人地址" align="center" prop="contactsAddress"></el-table-column>
					<el-table-column label="负责人" align="center" prop="responsiblePerson"></el-table-column>
					<el-table-column label="负责人电话(手机)" align="center" prop="responsiblePersonTel"></el-table-column>
					<el-table-column label="负责人邮箱" align="center" prop="responsiblePersonEmail"></el-table-column>
					<el-table-column v-if="geteditbefore" label="操作" fixed="right" header-align="center" align="center" width="90">
						<template #default="{ row, $index }">
							<!-- <el-icon style="color: #1491ff"><EditPen /></el-icon> -->
							<el-icon v-if="row.isedit" :size="16" color="#1491FF" class="cursor" @click="goedit2(row, $index)"> <Edit /></el-icon>
							<el-icon v-if="row.isedit" class="ml20 cursor" :size="16" style="color: #fe6360" @click="removeli_cb($index)"><Delete /></el-icon>
						</template>
					</el-table-column>
				</el-table>
			</el-tab-pane>
			<el-tab-pane label="主要起草人员" name="third">
				<el-button v-if="geteditbefore" type="primary" small @click="addqicao">新增</el-button>
				<el-table :data="queryParams.projectMainDrafterList" stripe class="mt20">
					<el-table-column label="序号" align="center" type="index" width="60">
						<template #default="{ $index }">
							{{ $index + 1 }}
						</template>
					</el-table-column>
					<el-table-column label="主要起草人员名称" align="center" prop="mainDrafterName" />

					<el-table-column label="职称/职务" align="center" prop="mainDrafterTitle" />

					<el-table-column label="单位" align="center" prop="mainDrafterCompany" />

					<el-table-column label="主要负责的章节或技术内容" align="center" prop="responsibleContent" />

					<el-table-column label="备注" align="center" prop="remark" />

					<el-table-column v-if="geteditbefore" label="操作" fixed="right" header-align="center" align="center" width="90">
						<template #default="{ row, $index }">
							<!-- <el-icon style="color: #1491ff"><EditPen /></el-icon> -->
							<el-icon v-if="row.isedit" :size="16" color="#1491FF" class="cursor" @click="goedit1(row, $index)"> <Edit /></el-icon>
							<el-icon v-if="row.isedit" class="ml20 cursor" :size="16" style="color: #fe6360" @click="removeli_qc($index)"><Delete /></el-icon>
						</template>
					</el-table-column>
				</el-table>
			</el-tab-pane>
		</el-tabs>
		<!-- {{ props.requestParams.progressStatus }} -->

		<!-- v-if="huihoushow" -->
		<el-form ref="queryForm" :inline="true" :model="queryParams" :rules="rules" class="mt20">
			<el-row>
				<el-col :span="8">
					<el-form-item prop="songsReviewTime" label="评审时间:" style="width: 80%">
						<el-date-picker
							v-model="queryParams.songsReviewTime"
							type="date"
							value-format="YYYY-MM-DD HH:mm:ss"
							format="YYYY-MM-DD"
							style="width: 100%"
							:disabled="!geteditbefore"
						/>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<!-- v-if="huihoushow" -->
		<div>
			<div class="titlezi flex">
				<div class="flex-1">评审阶段附件</div>
			</div>

			<div class="kuaibj mt20">
				<Cfupload
					v-for="value in fjcodelist1"
					:key="value"
					:istable="false"
					:labeltitle="value.definitionName"
					:code="value.businessCode"
					:suffix-type="value.suffixType"
					:disabled="geteditbefore"
					:filelist="value.uploadedFiles"
					:ismust="value.imustUpload"
					@success="handleUploadSuccessafter"
					@remove="handleFileRemoveafter"
				></Cfupload>

				<Zhuanjiatable :zjtablelist="queryParams.submitApprovalExperts" :isedit="geteditbefore" @getlist="getzjlist" />
			</div>
		</div>
		<!-- {{ props.requestParams.progressStage + 'sgs2222' }}
		{{ props.requestParams.progressStatus + 'sgsgg22' }} -->

		<!-- <div
			v-if="!getedit && Number(props.requestParams.progressStage) == 9"
			class="dialog-footer mt10"
			type="primary"
			plain
			@click="cancelSubmit('SubmitApproval_After', 23)"
		>
			<el-button>退回</el-button>
		</div> -->

		<div class="dialog-footer mt10">
			<!-- <el-button v-if="songshenshow" type="success" @click="zancunSubmit('SubmitApproval_Before')">暂存</el-button> -->
			<el-button v-if="shenbaoanshow" type="success" @click="zancunSubmit('SubmitApproval_After')">暂存</el-button>

			<!-- <el-button v-if="songshenshow" type="primary" @click="songshenSubmit">提请送审</el-button> -->
			<!-- <el-button v-if="showqd" type="primary" @click="shanxianSubmit">确定</el-button> -->
			<el-button v-if="shenbaoanshow" type="primary" @click="endSubmit">提交</el-button>
			<!-- <el-button v-if="showqd" type="primary" plain @click="cancelSubmit('SubmitApproval_Before', 22)">退回</el-button> -->
			<el-button v-if="backshow1" type="primary" plain @click="cancelSubmit1">退回</el-button>
		</div>
	</div>

	<addUnit v-model="showAddUnit" :unit-count="unitCount" :unit-info="unitInfo" @submit-success="submitUnit" @close-dialog="closeUnitDialog" />
	<addUnit1 v-model="showAddUnit1" :unit-count="unitCount1" :unit-info="unitInfo1" @submit-success="submitUnit1" @close-dialog="closeUnitDialog1" />
	<addDrafer v-model="showAddDrafter" :drafter-info="drafterInfo" @submit-success="submitDrafter" @close-dialog="closeDrafterDialog" />
</template>
<script setup lang="ts">
import { ref, defineProps, watch, nextTick, onMounted, computed, reactive, h } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import Cfupload from '@/components/cf-upload/index.vue'
import { router } from '@/router'
import {
	progressInfoApi,
	basicInfoApi,
	queryFilesSettingApi,
	updateStageAndStatusApi,
	doReturnApi,
	updateProgressApi,
	changeStageAndStatusApi
} from '@/api/bianzhi/index'
import Zhuanjiatable from './zhuanjiatable.vue'
import { Drafter, Unit } from '@/interface/declare'
import { useDictionaryStore } from '@/store/modules/dictionary'
import { get } from 'sortablejs'
import { is } from '@babel/types'
import { useUserStore } from '@/store/modules/user'
import AddUnit from './addUnit.vue'
import AddUnit1 from './addUnit1.vue'

import AddDrafer from './addDrafer.vue'
const userStore = useUserStore()
const emit = defineEmits<{ (event: 'updateData', value: ''): void }>()
const hanvisible = ref(false)
const activeName = ref('first')
const queryParams = ref({ projectMainDrafterList: [], partUnitList: [], mainUnitList: [], submitApprovalExperts: [] } as any)
const dictionaryStore = useDictionaryStore()
const jdstatuslist = dictionaryStore.getDictionaryByCode('T007')
const queryForm = ref(null)
const rules = reactive({
	songsReviewTime: [{ required: true, message: '请选择时间', trigger: 'change' }]
})

const unitInfo = ref<Unit>({
	id: '',
	projectInfoId: '',
	orderName: 1,
	editorUnitName: '',
	userCode: '',
	contacts: '',
	contactsTel: '',
	contactsEmail: '',
	contactsAddress: '',
	responsiblePerson: '',
	responsiblePersonTel: '',
	responsiblePersonEmail: '',
	unitType: 0,
	isedit: true
})
const unitInfo1 = ref<Unit>({
	id: '',
	projectInfoId: '',
	orderName: 1,
	editorUnitName: '',
	userCode: '',
	contacts: '',
	contactsTel: '',
	contactsEmail: '',
	contactsAddress: '',
	responsiblePerson: '',
	responsiblePersonTel: '',
	responsiblePersonEmail: '',
	unitType: 1,
	isedit: true
})
const drafterInfo = ref<Drafter>({
	mainDrafterName: '',
	mainDrafterTitle: '',
	mainDrafterCompany: '',
	responsibleContent: '',
	drafterRemark: '',
	remark: '',
	// submitApprovalDelete: 1,
	index: -1
})
const props = defineProps({
	datainfo: {
		type: Object,
		default: () => ({})
	},
	requestParams: {
		type: Object,
		default: () => ({})
	}
})

watch(
	() => props.requestParams,
	(newVal: any, oldVal: any) => {
		queryParams.value = props.requestParams || {}
		console.log(queryParams.value, '我是queryParams')

		if (props.requestParams.mainUnitList.length > 0) {
			queryParams.value.mainUnitList = props.requestParams.mainUnitList.map((item: any) => {
				const aa = {
					...item,
					isedit: item.isPublish === 1 ? false : true
				}
				return aa
			})
		}
		if (props.requestParams.partUnitList.length > 0) {
			queryParams.value.partUnitList = props.requestParams.partUnitList.map((item: any) => {
				const aa = {
					...item,
					isedit: item.isPublish === 1 ? false : true
				}
				return aa
			})
		}
		if (props.requestParams.projectMainDrafterList.length > 0) {
			queryParams.value.projectMainDrafterList = props.requestParams.projectMainDrafterList.map((item: any) => {
				const aa = {
					...item,
					isedit: item.isPublish === 1 ? false : true
				}
				return aa
			})
		}

		///	console.log( props.requestParams.progressStatus, '我是props的datainfo')
		queryFilesSettingApi({ businessStage: 'submitApproval_Before' }).then(res => {
			fjcodelist.value = res.data
			nextTick(() => {
				console.log(props.requestParams.submitApprovalBeforeFileList, '我是大纲附件们')

				//	console.log(props.requestParams.outlinePreparationFileList, '我是大纲附件们')
				///  console.log(props.requestParams.solicitOpinionsBeforeFileList.length, '我是大纲附件们' )
				if (props.requestParams.submitApprovalBeforeFileList.length > 0) {
					const aa: any = props.requestParams.submitApprovalBeforeFileList
					uploadedFilesbefore.value = props.requestParams.submitApprovalBeforeFileList
					const result = fjcodelist.value.map(itemA => {
						const matchedItem = aa.find(itemB => itemB.remark === itemA.definitionName)
						//	console.log(matchedItem, 'matchedItem')
						if (matchedItem) {
							return { ...itemA, uploadedFiles: [matchedItem] } // 合并相同字段的数据
						}
						return itemA
					})
					fjcodelist.value = result //result赋值给 fjcodelist
				}
			})
		})

		if (Number(props.requestParams.progressStatus) >= 5) {
			queryFilesSettingApi({ businessStage: 'submitApproval_After' }).then(res => {
				fjcodelist1.value = res.data
				nextTick(() => {
					//	console.log(props.requestParams.outlinePreparationFileList, '我是大纲附件们')

					if (props.requestParams.submitApprovalAfterFileList.length > 0) {
						const aa: any = props.requestParams.submitApprovalAfterFileList
						uploadedFilesafter.value = props.requestParams.submitApprovalAfterFileList
						const result1 = fjcodelist1.value.map(itemA => {
							const matchedItem = aa.find(itemB => itemB.remark === itemA.definitionName)
							//	console.log(matchedItem, 'matchedItem')
							if (matchedItem) {
								return { ...itemA, uploadedFiles: [matchedItem] } // 合并相同字段的数据
							}
							return itemA
						})
						fjcodelist1.value = result1 //result赋值给 fjcodelist
						//	console.log('fjcode', fjcodelist.value)
					}
				})
			})
		}
	},
	{ immediate: true }
)
const fjcodelist = ref(null)
const fjcodelist1 = ref(null)
const datalist = ref([])
const uploadedFilesafter = ref([])
const uploadedFilesbefore = ref([])
// (Number(props.requestParams.progressStatus) == 6 || Number(props.requestParams.progressStatus) == 8)
const shenbaoanshow = computed(() => {
	if (userStore.user.roleCode.includes('SB')) {
		if (Number(props.requestParams.progressStatus) < 9 && props.datainfo.edit == 'true') {
			return true
		} else {
			return false
		}
	} else {
		return false
	}
})

const backshow1 = computed(() => {
	if (props.datainfo.edit == 'true' && !userStore.user.roleCode.includes('SHZJW')) {
		if (Number(props.requestParams.progressStatus) == 9 || Number(props.requestParams.progressStatus) == 24) {
			return true
		} else {
			return false
		}
	} else {
		return false
	}
})

const huihoushow = computed(() => {
	if (userStore.user.roleCode.includes('ZZADMIN') || userStore.user.roleCode.includes('SHZZ') || userStore.user.roleCode.includes('SYADMIN')) {
		if (Number(props.requestParams.progressStatus) >= 9 && Number(props.requestParams.progressStatus) != 22) {
			return true
		} else {
			return false
		}
	} else {
		if (Number(props.requestParams.progressStatus) >= 8 && Number(props.requestParams.progressStatus) != 22) {
			return true
		} else {
			return false
		}
	}
})

const showqd = computed(() => {
	if (userStore.user.roleCode.includes('ZZADMIN') || userStore.user.roleCode.includes('SHZZ') || userStore.user.roleCode.includes('SYADMIN')) {
		if (props.datainfo.edit == 'true') {
			// || Number(props.requestParams.progressStatus) == 23
			if (Number(props.requestParams.progressStatus) == 7) {
				return true
			} else {
				return false
			}
		} else {
			return false
		}
	} else {
		return false
	}
})
// const geteditbefore = computed(() => {
// 	if (userStore.roleCode.includes('SB')) {
// 		if (Number(props.requestParams.progressStage) == 8) {
// 			if (props.datainfo.edit == 'true' && (Number(props.requestParams.progressStatus) == 22 || Number(props.requestParams.progressStatus) < 7)) {
// 			} else {
// 				return false
// 			}
// 		} else {
// 			return false
// 		}
// 	} else {
// 		return false
// 	}
// })
const geteditbefore = computed(() => {
	if (userStore.user.roleCode.includes('SB')) {
		if (Number(props.requestParams.progressStage) == 8) {
			if (props.datainfo.edit == 'true') {
				if (Number(props.requestParams.progressStatus) == 22 || Number(props.requestParams.progressStatus) < 9) {
					return true
				} else {
					return false
				}
			} else {
				return false
			}
		} else {
			return false
		}
	} else {
		return false
	}
})
const getedit = computed(() => {
	if (userStore.user.roleCode.includes('SB')) {
		if (Number(props.requestParams.progressStage) == 8) {
			if (props.datainfo.edit == 'true') {
				return true
			} else {
				return false
			}
		} else {
			if (Number(props.requestParams.progressStatus) == 22 || Number(props.requestParams.progressStatus) == 23) {
				return true
			} else {
				return false
			}
		}
	} else {
		return false
	}
})

const backshow = computed(() => {
	if (Number(props.requestParams.progressStatus) == 22 || Number(props.requestParams.progressStatus) == 23) {
		return true
	} else {
		return false
	}
})
// || Number(props.requestParams.progressStatus) == 22 || Number(props.requestParams.progressStatus) == 23
const songshenshow = computed(() => {
	if (userStore.user.roleCode.includes('SB')) {
		if (Number(props.requestParams.progressStatus) == 6 || Number(props.requestParams.progressStatus) == 22) {
			if (props.datainfo.edit == 'true') {
				return true
			} else {
				return false
			}
		} else {
			return false
		}
	} else {
		return false
	}
})
const showAddUnit = ref(false)
const showAddUnit1 = ref(false)

const submitUnit = (e: Unit) => {
	showAddUnit.value = false
	const index = e.index ?? -1
	if (index > -1) {
		queryParams.value.mainUnitList[index] = e
	} else {
		queryParams.value.mainUnitList.push({ ...e })
		//	console.log(queryParams.value.mainUnitList, '我是mainUnitList')
	}
}
const submitUnit1 = (e: Unit) => {
	showAddUnit1.value = false
	console.log(e, 'e')
	const index = e.index ?? -1
	if (index > -1) {
		queryParams.value.partUnitList[index] = e
	} else {
		queryParams.value.partUnitList.push({ ...e })
		//	console.log(queryParams.value.partUnitList.value, '我是partUnitList')
	}
}

const cancelSubmit1 = () => {
	ElMessageBox.confirm('是否确认退回', '确认退回', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning'
	})
		.then(() => {
			const data = {
				id: props.requestParams.id,
				progressStage: 8,
				progressStatus: 8
			}
			changeStageAndStatusApi(data).then(res => {
				emit('updateData', '')
				ElMessage.success('退回成功')
			})
		})
		.catch(() => {})
}
const unitCount = ref(0)
const unitCount1 = ref(0)

const closeUnitDialog = () => {
	unitCount.value = 0
	unitInfo.value = {
		orderName: 1,
		editorUnitName: '',
		contacts: '',
		contactsTel: '',
		contactsEmail: '',
		contactsAddress: '',
		responsiblePerson: '',
		responsiblePersonTel: '',
		responsiblePersonEmail: '',
		userCode: '',
		remark: '',
		unitType: 0,
		id: '',
		index: -1,
		isedit: true
	}
}
const closeUnitDialog1 = () => {
	unitCount1.value = 0
	unitInfo1.value = {
		orderName: 1,
		editorUnitName: '',
		contacts: '',
		contactsTel: '',
		contactsEmail: '',
		contactsAddress: '',
		responsiblePerson: '',
		responsiblePersonTel: '',
		responsiblePersonEmail: '',
		userCode: '',
		remark: '',
		unitType: 1,
		id: '',
		index: -1,
		isedit: true
	}
}
const closeDrafterDialog = () => {
	drafterInfo.value = {
		mainDrafterName: '',
		mainDrafterTitle: '',
		mainDrafterCompany: '',
		responsibleContent: '',
		drafterRemark: '',
		remark: '',
		id: '',
		index: -1,
		isedit: true
	}
}

const goedit = (e: Unit, i: number) => {
	showAddUnit.value = true
	e.index = i
	unitInfo.value = e
}

const goedit2 = (e: Unit, i: number) => {
	showAddUnit1.value = true
	e.index = i
	unitInfo1.value = e
}
const goedit1 = (e: Unit, i: number) => {
	showAddDrafter.value = true
	e.index = i
	drafterInfo.value = e
}
const submitDrafter = (e: Drafter) => {
	showAddDrafter.value = false
	const index = e.index ?? -1
	if (index > -1) {
		queryParams.value.projectMainDrafterList[index] = e
	} else {
		queryParams.value.projectMainDrafterList.push({ ...e })
	}
}
const getzjlist = value => {
	queryParams.value.submitApprovalExperts = value
}
const validateBeforeSubmit = (requiredItems, uploadedFiles) => {
	const errors = []

	requiredItems.forEach(item => {
		const uploadedItem = uploadedFiles.some(file => file.remark === item.definitionName)
		if (!uploadedItem && item.imustUpload === 1) {
			errors.push(`【${item.definitionName}】未上传文件`)
		}
	})

	if (errors.length > 0) {
		ElMessage.error(errors.join(','))
		return false
	}

	return true
}

// 处理上传成功事件
const handleUploadSuccessafter = (data: any) => {
	//	console.log(uploadedFilesafter.value, 'wwwwttttttttttttt')
	uploadedFilesafter.value.push(data) // 将接口返回的数据添加到文件列表

	//	console.log(uploadedFilesafter.value, '111uploadedFilesafterwwwwe')
}

// 处理文件删除事件
const handleFileRemoveafter = (file: any) => {
	const id = file.response ? file.response.data.id : file.id
	uploadedFilesafter.value = uploadedFilesafter.value.filter(
		item => item.id !== id // 根据 uid 删除对应文件
	)

	//console.log(uploadedFilesafter.value, '22222222222222tt')
}

// 处理上传成功事件
const handleUploadSuccessbefore = (data: any) => {
	uploadedFilesbefore.value = [...uploadedFilesbefore.value, data]
	//	uploadedFilesbefore.value.push(data) // 将接口返回的数据添加到文件列表
}

// 处理文件删除事件
const handleFileRemovebefore = (file: any) => {
	//console.log(file, 'file')
	//console.log(uploadedFilesbefore.value, 'uploadedFilesafter')
	const id = file.response ? file.response.data.id : file.id
	uploadedFilesbefore.value = uploadedFilesbefore.value.filter(
		item => item.id !== id // 根据 uid 删除对应文件
	)

	//	console.log(uploadedFilesbefore.value, '222226666666666666666666uploadedFilesafter')
}
const newadditem = () => {
	const list: any = {
		expertName: '',
		expertTitle: '',
		expertCompany: '',
		expertTel: ''
	}
	datalist.value.push(list)
}

const removeli_cb = (index: any) => {
	queryParams.value.partUnitList.splice(index, 1)
}

const removeli_zb = (index: any) => {
	queryParams.value.mainUnitList.splice(index, 1)
}

const removeli_qc = (index: any) => {
	queryParams.value.projectMainDrafterList.splice(index, 1)
}
const istype = ref(0)
const showAddDrafter = ref(false)

const addzhubian = () => {
	// const list: any = {
	// 	id: '',
	// 	projectInfoId: '',
	// 	orderName: '第' + (queryParams.value.mainUnitList.length + 1) + '参编单位',
	// 	editorUnitName: '',
	// 	userCode: '',
	// 	contacts: '',
	// 	contactsTel: '',
	// 	contactsEmail: '',
	// 	contactsAddress: '',
	// 	responsiblePerson: '',
	// 	responsiblePersonTel: '',
	// 	responsiblePersonEmail: '',
	// 	unitType: 0,
	// 	isedit: true
	// }
	if (queryParams.value.mainUnitList.length >= 2) {
		ElMessageBox.confirm('依据编制要求建议主编单位不超过2个！', '温馨提示', {
			confirmButtonText: '继续增加',
			cancelButtonText: '关闭',
			type: 'warning'
		})
			.then(() => {
				unitCount.value = queryParams.value.mainUnitList.length
				showAddUnit.value = true
				unitInfo.value.orderName = queryParams.value.mainUnitList.length + 1
			})
			.catch(() => {})
	} else {
		unitCount.value = queryParams.value.mainUnitList.length
		showAddUnit.value = true
		unitInfo.value.orderName = queryParams.value.mainUnitList.length + 1
	}
}

const addqicao = () => {
	const list: any = {
		id: '',
		mainDrafterName: '',
		mainDrafterTitle: '',
		mainDrafterCompany: '',
		responsibleContent: '',
		drafterRemark: '',
		remark: '',
		isedit: true
	}

	if (queryParams.value.projectMainDrafterList.length >= 25) {
		ElMessageBox.confirm('依据编制要求建议起草人员不超过25人！', '温馨提示', {
			confirmButtonText: '继续增加',
			cancelButtonText: '关闭',
			type: 'warning'
		})
			.then(() => {
				showAddDrafter.value = true
			})
			.catch(() => {})
	} else {
		console.log(252525252525)
		showAddDrafter.value = true
	}
}
const addcanbian = () => {
	// const list: any = {
	// 	id: '',
	// 	projectInfoId: '',
	// 	orderName: 1,
	// 	editorUnitName: '',
	// 	contacts: '',
	// 	userCode: '',
	// 	contactsTel: '',
	// 	contactsEmail: '',
	// 	contactsAddress: '',
	// 	responsiblePerson: '',
	// 	responsiblePersonTel: '',
	// 	responsiblePersonEmail: '',
	// 	unitType: 1
	// }

	if (queryParams.value.partUnitList.length >= 8) {
		ElMessageBox.confirm('依据编制要求建议参编单位不超过8家！', '温馨提示', {
			confirmButtonText: '继续增加',
			cancelButtonText: '关闭',
			type: 'warning'
		})
			.then(() => {
				unitCount1.value = queryParams.value.partUnitList.length
				unitInfo1.value.orderName = queryParams.value.partUnitList.length + 1
				showAddUnit1.value = true
			})
			.catch(() => {})
	} else {
		unitCount1.value = queryParams.value.partUnitList.length
		unitInfo1.value.orderName = queryParams.value.partUnitList.length + 1
		showAddUnit1.value = true
	}
}
const songshenSubmit = () => {
	if (!validateBeforeSubmit(fjcodelist.value, uploadedFilesbefore.value)) {
		return
	}
	if (queryParams.value.mainUnitList.length == 0) {
		ElMessage.error('请至少添加一条主编单位信息')
		return false
	}
	if (queryParams.value.projectMainDrafterList.length == 0) {
		ElMessage.error('请至少添加一条起草人员信息')
		return false
	}
	if (queryParams.value.partUnitList.length == 0) {
		ElMessage.error('请至少添加一条参编单位信息')
		return false
	}

	ElMessageBox.confirm('提交送审信息，确认是否送审', '确认送审', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning'
	})
		.then(() => {
			Emittion('SubmitApproval_Before', 7)
		})
		.catch(() => {})
}

const shanxianSubmit = () => {
	if (!validateBeforeSubmit(fjcodelist.value, uploadedFilesbefore.value)) {
		return
	}

	if (queryParams.value.mainUnitList.length == 0) {
		ElMessage.error('请至少添加一条主编单位信息')
		return false
	}
	if (queryParams.value.projectMainDrafterList.length == 0) {
		ElMessage.error('请至少添加一条起草人员信息')
		return false
	}
	if (queryParams.value.partUnitList.length == 0) {
		ElMessage.error('请至少添加一条参编单位信息')
		return false
	}

	ElMessageBox.confirm('确定送审信息，确认是否提交', '确认提交', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning'
	})
		.then(() => {
			Emittion('SubmitApproval_After', 8)
		})
		.catch(() => {})
}

const endSubmit = () => {
	if (!validateBeforeSubmit(fjcodelist1.value, uploadedFilesafter.value)) {
		return
	}

	if (queryParams.value.mainUnitList.length == 0) {
		ElMessage.error('请至少添加一条主编单位信息')
		return false
	}
	if (queryParams.value.projectMainDrafterList.length == 0) {
		ElMessage.error('请至少添加一条起草人员信息')
		return false
	}
	if (queryParams.value.partUnitList.length == 0) {
		ElMessage.error('请至少添加一条参编单位信息')
		return false
	}
	if (queryParams.value.submitApprovalExperts.length == 0) {
		ElMessage.error('请至少添加一条专家信息')
		return false
	}

	queryForm.value.validate(valid => {
		if (valid) {
			ElMessageBox.confirm('是否确认提交送审', '确认提交', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			})
				.then(() => {
					// Emittion('ApprovalApplication', 9)
					Emittion('ApprovalApplication', 9)
				})
				.catch(() => {})
		} else {
			console.log('error submit!!')
			return false
		}
	})
}

const cancelSubmit = (type: string, code: any) => {
	ElMessageBox.confirm('是否确认退回', '确认退回', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning'
	})
		.then(() => {
			godoReturn(type, code)
		})
		.catch(() => {})
}

const godoReturn = (type: string, code: number) => {
	//	const experlist = props.requestParams.solicitOpinionsExperts.map((it: any) => ({
	const data = {
		progressCode: type,
		id: props.requestParams.id,
		progressStatus: code
	}
	doReturnApi(data).then(res => {
		emit('updateData', '')
		ElMessage.success('退回成功')
		// nextTick(() => {
		// 	router.push({
		// 		path: '/bianzhi/shenbao/index'
		// 	})
		// })
	})
}

const Emittion = (type: string, code: number) => {
	const experlist = queryParams.value.submitApprovalExperts.map((it: any) => ({
		expertName: it.expertName,
		expertTitle: it.expertTitle,
		expertCompany: it.expertCompany,
		expertTel: it.expertTel,
		id: it.id ? it.id : ''
	}))
	if (uploadedFilesafter.value.length > 0) {
		uploadedFilesafter.value.forEach(item => {
			delete item.name
		})
	}
	if (uploadedFilesbefore.value.length > 0) {
		uploadedFilesbefore.value.forEach(item => {
			delete item.name
		})
	}

	const filelist = [...uploadedFilesafter.value, ...uploadedFilesbefore.value]
	arrdelet()
	const data = {
		expertsList: experlist,
		id: props.requestParams.id,
		standardProjectName: props.requestParams.name,
		standardNumber: props.requestParams.standardNumber,
		dgbzReviewTime: props.requestParams.dgbzReviewTime, //大纲评审时间  //大纲评审时间
		zqyjReviewTime: props.requestParams.zqyjReviewTime, //征求意见评审时间
		songsReviewTime: queryParams.value.songsReviewTime, //送审评审时间
		baopCompletionTime: props.requestParams.baopCompletionTime, //报批时间
		releaseTime: props.requestParams.releaseTime, //发布时间
		implementationTime: props.requestParams.implementationTime, //实施时间
		progressCode: type,
		progressStatus: code,
		routeName: '/progressInfo',
		filesRecordList: filelist, //所有附件
		mainUnitList: queryParams.value.mainUnitList, //主编单位
		partUnitList: queryParams.value.partUnitList, //参编单位
		projectMainDrafterList: queryParams.value.projectMainDrafterList //主要起草人员
	}

	updateStageAndStatusApi(data).then(res => {
		emit('updateData', '')
		ElMessage.success('提交成功')
		// nextTick(() => {
		// 	router.push({
		// 		path: '/bianzhi/shenbao/index'
		// 	})
		// })
	})
}

const zancunSubmit = (code: any) => {
	const experlist = queryParams.value.submitApprovalExperts.map((it: any) => ({
		expertName: it.expertName,
		expertTitle: it.expertTitle,
		expertCompany: it.expertCompany,
		expertTel: it.expertTel,
		id: it.id ? it.id : ''
	}))
	if (uploadedFilesafter.value.length > 0) {
		uploadedFilesafter.value.forEach(item => {
			delete item.name
		})
	}
	if (uploadedFilesbefore.value.length > 0) {
		uploadedFilesbefore.value.forEach(item => {
			delete item.name
		})
	}
	arrdelet()
	const filelist = [...uploadedFilesafter.value, ...uploadedFilesbefore.value]

	const data = {
		expertsList: experlist,
		id: props.requestParams.id,
		standardProjectName: props.requestParams.name,
		standardNumber: props.requestParams.standardNumber,
		dgbzReviewTime: props.requestParams.dgbzReviewTime, //大纲评审时间  //大纲评审时间
		zqyjReviewTime: props.requestParams.zqyjReviewTime, //征求意见评审时间
		songsReviewTime: queryParams.value.songsReviewTime, //送审评审时间
		baopCompletionTime: props.requestParams.baopCompletionTime, //报批时间
		releaseTime: props.requestParams.releaseTime, //发布时间
		implementationTime: props.requestParams.implementationTime, //实施时间
		progressCode: code,
		routeName: '/progressInfo',
		filesRecordList: filelist, //所有附件
		mainUnitList: queryParams.value.mainUnitList, //主编单位
		partUnitList: queryParams.value.partUnitList, //参编单位
		projectMainDrafterList: queryParams.value.projectMainDrafterList //主要起草人员
	}
	updateProgressApi(data).then(res => {
		ElMessage.success('暂存成功')
		emit('updateData', '')
		// nextTick(() => {
		// 	router.push({
		// 		path: '/bianzhi/shenbao/index'
		// 	})
		// })
	})
}

const arrdelet = () => {
	if (queryParams.value.mainUnitList.length > 0) {
		queryParams.value.mainUnitList.forEach(item => {
			delete item.isedit
			delete item.index
		})
	}

	if (queryParams.value.partUnitList.length > 0) {
		queryParams.value.partUnitList.forEach(item => {
			delete item.isedit
			delete item.index
		})
	}

	if (queryParams.value.projectMainDrafterList.length > 0) {
		queryParams.value.projectMainDrafterList.forEach(item => {
			delete item.isedit
			delete item.index
		})
	}
}
</script>

<style lang="scss" scoped>
.kuaibj {
	background-color: #f5f6fa;
	padding: 20px;
	border-radius: 6px;
}
</style>
